package com.snail.billing.page;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.android.net.HttpApp;
import com.android.net.HttpResult;
import com.android.net.HttpSession;
import com.android.net.OnHttpResultListener;
import com.android.util.AlertUtil;
import com.android.util.ResUtil;
import com.snail.billing.Billing;
import com.snail.billing.BillingCallback;
import com.snail.billing.DataCache;
import com.snail.billing.Resource;
import com.snail.billing.data.Account;
import com.snail.billing.data.AccountManager;
import com.snail.billing.json.JsonBase;
import com.snail.billing.net.BillingEncode;
import com.snail.billing.os.DialogPage;
import com.snail.billing.session.BindAccountSession;
import com.snail.billing.util.AccountCaptcha;

public class BindAccountPage extends DialogPage implements View.OnClickListener, OnHttpResultListener {

	private View buttonClose;

	private EditText inputAccount;
	private EditText inputPwd;
	private View button;

	private HttpApp httpApp;
	private BindAccountSession bindAccountSession;

	private String accountString, pwdString;
	
	private int state;

	@Override
	public int createLayoutId() {
		return ResUtil.getLayoutId(Resource.layout.snailbilling_bind_account_activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		buttonClose = findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_button_close));
		buttonClose.setOnClickListener(this);
		//
		if (getPageArgs() != null) {
			if (getPageArgs().containsKey("state")) {
				state = getPageArgs().getInt("state");
			}
		}
		inputAccount = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_bind_account_input_account));
		inputPwd = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_bind_account_input_pwd));
		button = findViewById(ResUtil.getViewId(Resource.id.snailbilling_bind_account_button));
		button.setOnClickListener(this);
		//
		httpApp = new HttpApp(getContext());
		httpApp.setOnHttpResultListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v.equals(buttonClose)) {
			getPageManager().finish();
		} else if (v.equals(button)) {
			accountString = inputAccount.getText().toString();
			if (TextUtils.isEmpty(accountString)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_launch_input_account));
				return;
			}
			pwdString = inputPwd.getText().toString();
			if (TextUtils.isEmpty(pwdString)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_launch_input_pwd));
				return;
			}
			if (!AccountCaptcha.validateAccount(getContext(), accountString)) {
				return;
			}
			if (!AccountCaptcha.validatePwd(getContext(), accountString, pwdString, pwdString)) {
				return;
			}
			bindAccountSession = new BindAccountSession(accountString, pwdString);
			httpApp.request(bindAccountSession);
		}
	}

	@Override
	public void onHttpResult(HttpResult httpResult) {
		if (httpResult.isSucceed()) {
			HttpSession httpSession = httpResult.getHttpSession();
			if (httpSession.equals(bindAccountSession)) {
				JsonBase jsonData = new JsonBase((String) httpSession.getResponseData());
				if (jsonData.getCode() == 1) {
					Account currentAccount = AccountManager.getCurrentAccount();
					String oldAccountString = currentAccount.getAccount();
					String oldPwdString = currentAccount.getPwd();
					currentAccount.setAccount(accountString);
					currentAccount.setPwd(pwdString);
					AccountManager.setBindAccount(currentAccount);
					if (DataCache.getInstance().callbackIsMd5) {
						String action = BillingCallback.ACTION_BIND_ACCOUNT_PWD_MD5;
						DataCache.getInstance().importParams.billingCallback.onCallback(1, action, new String[] { accountString, BillingEncode.MD5(pwdString), oldAccountString, BillingEncode.MD5(oldPwdString) });
					} else {
						String action = BillingCallback.ACTION_BIND_ACCOUNT;
						DataCache.getInstance().importParams.billingCallback.onCallback(1, action, new String[] { accountString, pwdString, oldAccountString, oldPwdString });
					}
					if (state == 0) {
						getPageManager().finish();
						DataCache.getInstance().importParams.pageClass = DialogChangePage.class;
						Intent intent = new Intent(getContext(), Billing.class);
						intent.putExtra(DialogChangePage.ACCOUNT, accountString);
						intent.putExtra(DialogChangePage.PWD, pwdString);
						intent.putExtra(DialogChangePage.TYPE, Account.TYPE_COMMON);
						getContext().startActivity(intent);
					} else {
						getPageManager().backward();
					}
				} else {
					Toast.makeText(getContext(), "通行证已存在，请重新输入。", Toast.LENGTH_SHORT).show();
				}
			}
		}
	}

}
